package com.dd.common.utils;

import com.dd.common.enums.ErrorEnum;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;

@Data
@Schema(name = "响应对象工具类")
public class AjaxResult<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    @Schema(description = "响应码", example = "200")
    private Integer code;

    @Schema(description = "响应消息", example = "操作成功")
    private String message;

    @Schema(description = "响应数据, 可能为 null")
    private T data;

    @Schema(description = "时间戳", example = "1640995200000")
    private String timestamp;

    public AjaxResult(Integer code, String message) {
        this.code = code;
        this.message = message;
//        this.timestamp = System.currentTimeMillis();
        this.timestamp = LocalDateTime.now(ZoneId.of("Asia/Shanghai"))
                .format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }

    public AjaxResult(Integer code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
//        this.timestamp = System.currentTimeMillis();
        this.timestamp = LocalDateTime.now(ZoneId.of("Asia/Shanghai"))
                .format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }

    /**
     * 成功响应
     *
     * @return R对象
     */
    public static AjaxResult<Object> success() {
        return new AjaxResult<>(ErrorEnum.SUCCESS.getCode(), ErrorEnum.SUCCESS.getMsg());
    }

    /**
     * 成功响应且返回自定义消息
     *
     * @param msg 自定义消息
     * @return R对象
     */
    public static AjaxResult<Object> success(String msg) {
        return new AjaxResult<>(ErrorEnum.SUCCESS.getCode(), msg);
    }

    /**
     * 成功响应且返回数据
     *
     * @param data 数据
     * @return R对象
     */
    public static <T> AjaxResult<T> success(T data) {
        return new AjaxResult<>(ErrorEnum.SUCCESS.getCode(), ErrorEnum.SUCCESS.getMsg(), data);
    }

    /**
     * 成功响应且返回数据和自定义消息
     *
     * @param data 数据
     * @param msg  自定义消息
     * @return R对象
     */
    public static <T> AjaxResult<T> success(T data, String msg) {
        return new AjaxResult<>(ErrorEnum.SUCCESS.getCode(), msg, data);
    }

    /**
     * 错误响应，默认返回系统内部错误
     */
    public static AjaxResult<Object> fail() {
        return new AjaxResult<>(ErrorEnum.SYSTEM_ERROR.getCode(), ErrorEnum.SYSTEM_ERROR.getMsg());
    }

    /**
     * 根据错误枚举返回失败响应
     */
    public static AjaxResult<Object> fail(ErrorEnum error) {
        return new AjaxResult<>(error.getCode(), error.getMsg());
    }

    /**
     * 失败响应并允许覆盖消息
     */
    public static AjaxResult<Object> fail(ErrorEnum error, String msg) {
        return new AjaxResult<>(error.getCode(), msg != null ? msg : error.getMsg());
    }

    /**
     * 失败响应
     */
    public static AjaxResult<Object> fail(int code, String msg) {
        return new AjaxResult<>(code, msg);
    }
}
